package cn.kgc.tangcco.controller;

import cn.kgc.tangcco.config.JdbcConf;
import cn.kgc.tangcco.config.JdbcConfig;
import cn.kgc.tangcco.model.ResponseText;
import cn.kgc.tangcco.pojo.Student;
import cn.kgc.tangcco.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/12/26 10:10
 */
@RestController
@RequestMapping(value = "/test")
@EnableConfigurationProperties(JdbcConfig.class)
public class TestController {

    @Value("${jdbc.diver}")
    private String diver;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    @Autowired
    private JdbcConf jdbcConf;

    @Autowired
    private JdbcConfig jdbcConfig;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @GetMapping(value = "/jdbcParam")
    public Map<String, String> jdbcParam() {
        Map<String, String> params = new HashMap<>();
        params.put("diver", diver);
        params.put("url", url);
        params.put("username", username);
        params.put("password", password);
        return params;
    }

    @GetMapping(value = "/jdbcConf")
    public JdbcConf jdbcConf() {
        return jdbcConf;
    }

    @GetMapping(value = "/jdbcConfig")
    public JdbcConfig jdbcConfig() {
        return jdbcConfig;
    }

    @GetMapping(value = "/date")
    public StudentVo date() {
        StudentVo studentVo = new StudentVo();
        return studentVo.setBirthday(new Date());
    }

    @GetMapping(value = "/getConnection")
    public void getConnection() throws SQLException {
        System.out.println(dataSource.getClass());
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }
    @GetMapping("/query")
    public List<Map<String, Object>> map() {
        List<Map<String, Object>> students = jdbcTemplate.queryForList("select * FROM student");
        return students;
    }

}
